// pages/index/lottery.js
const app = getApp()
var Util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    lottery_info: {},
    token: '',
    banner_height: 300, //轮播图高度
    showPopup: false,
    is_success: 2,
    msg: '失败原因'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      token: app.globalData.token
    });
    var id = options.id || 0
    if (!(id)) {
      tt.showToast({
        title: '打开异常',
        icon: 'none',
        duration: 900
      });
      setTimeout(function () {
        tt.navigateBack({
          delta: 1,
        });
      }, 1000);

    } else {
      that.setData({
        id: id
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    var that = this;
    that.getLottery();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (option) {
    var _this = this;
    var lottery_info = _this.data.lottery_info;

    return {
      // query: 'k1=v1&ke=v2'
      title: _this.data.lottery_info.title,
      desc: _this.data.lottery_info.description,
      path: '/pages/index/lottery?id=' + _this.data.lottery_info.id, // ?后面的参数会在转发页面打开时传入onLoad方法
      imageUrl: _this.data.lottery_info.image, // 支持本地或远程图片，默认是小程序icon
      success: function () {
        console.log('转发发布器已吊起，并不意味着用户转发成功，微头条不提供这个时机的回调');
        tt.request({
          url: app.globalData.url + 'index.php?act=toutiao_member&op=addLottery&task_id=' + _this.data.id + '&address_id=' + lottery_info.address_id + '&key=' + encodeURI(app.globalData.token),
          header: {
            'content-type': 'application/json'
          },
          success(res) {
            tt.hideLoading({});
            if (res.data.code == 200) {
              //修改转发状态
              lottery_info.is_add = 1;
              lottery_info.add_count++;
              _this.setData({
                lottery_info: lottery_info,
                showPopup: true,
                is_success: 1,
                msg: lottery_info.show_title
              })
              // tt.showToast({
              //   title: '参与抽奖成功',
              //   duration: 2000,
              // });
            } else {
              // tt.showToast({
              //   title: res.data.datas.error,
              //   duration: 2000,
              // });
              _this.setData({
                showPopup: true,
                is_success: 2,
                msg: res.data.datas.error
              })
            }
          },
          fail(res) {
            console.log(`request调用失败`);
          }
        })
      },
      fail: function () {
        console.log('转发发布器吊起失败');
      }
    }
  },

  /**
   * 获取抽奖详情
   */
  getLottery: function () {
    var _this = this;
    tt.showLoading({
      title: '加载中...'
    });
    var url = app.globalData.url + 'index.php?act=toutiao_index&op=getLottery&task_id=' + _this.data.id;
    if (app.globalData.token) {
      url = app.globalData.url + 'index.php?act=toutiao_member&op=getLottery&task_id=' + _this.data.id + '&key=' + encodeURI(app.globalData.token)
    }

    tt.request({
      url: url,
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        tt.hideLoading({});
        if (res.data.code == 200) {

          _this.setData({
            lottery_info: res.data.datas.info
          });

        }
      },
      fail(res) {
        console.log(`request调用失败`);
      }
    })
  },


  /**
   * desc 预览大图
   */
  show_image: function (e) {
    var f_index = e.currentTarget.dataset.f_index; //第几天评论
    var index = e.currentTarget.dataset.index;//第几章图片

    // var img_list = this.data.lottery_info.goods_eval_list[f_index].geval_image_1024[index];
    var img_list = (this.data.lottery_info.evaluate_list[f_index].geval_image_1024[index]);

    tt.previewImage({
      current: img_list, // 当前显示图片的http链接
      urls: this.data.lottery_info.evaluate_list[f_index].geval_image_1024 // 需要预览的图片http链接列表
    })
  },

  closeWindow: function () {
    this.setData({
      showPopup: false
    });
  }
})